//
//  MyMessageCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/1.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyMessageCell.h"
#import "MessageModel.h"
#import "TimeHelper.h"
@interface MyMessageCell()
@property (weak, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UIImageView *lineIV;
@property (weak, nonatomic) IBOutlet UILabel *timeL;
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UILabel *contentL;
@property (weak, nonatomic) IBOutlet UIButton *detailBtn;

@end
static NSString *identifier = @"MyMessageCell";
@implementation MyMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.view.layer.cornerRadius = 3;
    self.view.layer.borderColor  = kLineColor.CGColor;
    self.view.layer.borderWidth  = 1;
    self.lineIV.backgroundColor = kOrangeBg;
    self.timeL.textColor = kLabel153Color;
    self.titleL.textColor = kLabel16Color;
    self.contentL.textColor = kLabel102Color;
    
}
+(instancetype)cellNibWithTableView:(UITableView *)tableView
{
    MyMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"MyMessageCell" bundle:nil] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)setModel:(MessageModel *)model
{
    _model = model;
    if (model.icon.length == 0) {
        self.detailBtn.hidden = YES;
        self.lineIV.hidden = YES;
    }else {
        self.detailBtn.hidden = NO;
        self.lineIV.hidden = NO;
    }
    NSTimeInterval interval    = model.createtime/1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd HH:mm"];
    NSString *currentDateStr = [formatter stringFromDate: date];
    
    self.titleL.text = model.title;
  
    XLog(@"[TimeHelper getCurrentTime]:%@",[TimeHelper getCurrentTime]);
    /**
     Wed Nov 28 13:42:35 2018
     Wed Nov 28 00:00:00 2018
     
     Mon Nov 26 00:00:00 2018
     
     Wed Nov 14 13:38:09 2018
     
     */
    XLog(@"model.createtime:%ld",model.createtime);//1543334400000 1543161600000 1542173889000
    XLog(@"[TimeHelper currentZeroTime]:%@",[TimeHelper currentZeroTime]);//1543334400000
    XLog(@"date:%@",date);
//86400000  1543248000000
    XLog(@"[[TimeHelper currentZeroTime] integerValue]-24*60*60*1000:%ld",[[TimeHelper currentZeroTime] integerValue]-24*60*60*1000);//1543248000000
                                     //1543161600000
    if (model.createtime >=[[TimeHelper currentZeroTime] integerValue]  ) {//今天
        [formatter setDateFormat:@"HH:mm"];
        NSString *currentDateStr = [formatter stringFromDate: date];
       self.timeL.text = [NSString stringWithFormat:@"今天:%@",currentDateStr];
    }else if (model.createtime >=[[TimeHelper currentZeroTime] integerValue]-24*60*60*1000&& model.createtime<[[TimeHelper currentZeroTime] integerValue] ){//昨天
        self.timeL.text = [NSString stringWithFormat:@"昨天:%@",currentDateStr];
    }else {
        self.timeL.text = currentDateStr;
    }
    self.contentL.text = model.content;
    
    if (1543248000000>1543161600000){
        XLog(@"ok");
    }else {
        XLog(@"no");
    }
}
- (IBAction)clickBtn:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(myMessageCell:withClickBtn:withIndexPath:)]) {
        [self.delegate myMessageCell:self withClickBtn:sender withIndexPath:self.indexPath];
    }
}
- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
}
@end
